package csu.edu.flatprogram.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import csu.edu.flatprogram.common.CommonResponse;
import csu.edu.flatprogram.entity.Item;
import csu.edu.flatprogram.entity.ItemInventory;
import csu.edu.flatprogram.entity.Product;
import csu.edu.flatprogram.persistence.ItemInventoryMapper;
import csu.edu.flatprogram.persistence.ItemMapper;
import csu.edu.flatprogram.persistence.ProductMapper;
import csu.edu.flatprogram.service.ItemService;
import csu.edu.flatprogram.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("itemService")
public class ItemServiceImpl implements ItemService {
    @Autowired
    public ProductMapper productMapper;
    @Autowired
    public ItemMapper itemMapper;
    @Autowired
    public ItemInventoryMapper itemInventoryMapper;

    @Override
    public CommonResponse<List<ItemVO>> getItemsByProductId(String productId){
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productId",productId);
        List<Item> itemList = itemMapper.selectList(queryWrapper);
        if (itemList.isEmpty()){
            return CommonResponse.createForError("该product下没有item商品");
        }
        Product product = productMapper.selectById(productId);
        List<ItemVO> itemVOList = new ArrayList<>();
        for (Item item : itemList){
            ItemVO itemVO = itemToItemVO(item,product);
            itemVOList.add(itemVO);
        }
        return CommonResponse.createForSuccess(itemVOList);
    }
    //查询库存
    private ItemVO itemToItemVO(Item item,Product product){
        ItemVO itemVO = new ItemVO();
        itemVO.setItemId(item.getItemId());
        itemVO.setProductId(item.getProductId());
        itemVO.setListPrice(item.getListPrice());
        itemVO.setUnitCost(item.getUnitCost());
        itemVO.setStatus(item.getStatus());
        itemVO.setAttribute1(item.getAttribute1());
        itemVO.setDescn(item.getDescn());
        itemVO.setDescnpic(item.getDescnpic());
        itemVO.setItemName(item.getItemName());

        itemVO.setCategoryId(product.getCategoryId());
        itemVO.setProductName(product.getName());
        itemVO.setProductDescription(product.getDescription());

        ItemInventory itemInventory = itemInventoryMapper.selectById(item.getItemId());
        itemInventory.setQuantity(itemInventory.getQuantity());
        itemVO.setQuantity(itemInventory.getQuantity());
        return itemVO;
    }
    @Override
    public CommonResponse<ItemVO> addItem(ItemVO itemVO){

        Item item = new Item();
        item.setItemId(itemVO.getItemId());
        item.setProductId(itemVO.getProductId());
        item.setListPrice(itemVO.getListPrice());
        item.setUnitCost(itemVO.getUnitCost());
        item.setAttribute1(itemVO.getAttribute1());
        item.setStatus(itemVO.getStatus());
        item.setDescn(itemVO.getDescn());
        item.setDescnpic(itemVO.getDescnpic());
        item.setItemName(itemVO.getItemName());

        System.out.println(item.getItemName()+"********************************");

        if(itemMapper.selectById(item.getItemId())!=null){
            return CommonResponse.createForError("商品ID重复");
        }else {
            // ItemInventory itemInventory = new ItemInventory();
            // itemInventory.setItemId(itemVO.getItemId());
            // itemInventory.setQuantity(itemVO.getQuantity());
            itemMapper.insert(item);
            // itemInventoryMapper.insert(itemInventory);
            return CommonResponse.createForSuccess("新建商品成功",itemVO);
        }
    }

    //删除商品
    @Override
    public CommonResponse<ItemVO> deleteItem(ItemVO itemVO){
        int rows = itemMapper.selectCount(Wrappers.<Item>query().eq("itemid",itemVO.getItemId()));
        if(rows==0) {
            return CommonResponse.createForSuccess("无该商品",itemVO);
        }else {
            itemMapper.delete(Wrappers.<Item>query().eq("itemid",itemVO.getItemId()));
            itemInventoryMapper.delete(Wrappers.<ItemInventory>query().eq("itemid",itemVO.getItemId()));
            return CommonResponse.createForError("删除商品成功");
        }

    }

    //更新商品信息
    @Override
    public CommonResponse<String> updateItem(Item item){
        int rows = itemMapper.selectCount(Wrappers.<Item>query().eq("itemid",item.getItemId()));
        if (rows==0){
            return CommonResponse.createForError("该商品不存在");
        }
        System.out.println(item.getItemName()+"********************************");
        item.setItemId(item.getItemId());
        item.setProductId(item.getProductId());
        item.setListPrice(item.getListPrice());
        item.setUnitCost(item.getUnitCost());
        item.setAttribute1(item.getAttribute1());
        item.setStatus(item.getStatus());
        item.setDescn(item.getDescn());
        item.setDescnpic(item.getDescnpic());
        item.setItemName(item.getItemName());

        if(itemMapper.selectById(item.getItemId())==null){
            return CommonResponse.createForError("商品不存在");
        }else {
            ItemInventory itemInventory = new ItemInventory();
            itemInventory.setItemId(item.getItemId());
            itemInventory.setQuantity(itemInventory.getQuantity());
            itemMapper.update(item,Wrappers.<Item>query().eq("itemid",item.getItemId()));
            itemInventoryMapper.updateById(itemInventory);
            return  CommonResponse.createForSuccess("更新成功");
        }

    }
}
